<template>
    <div class="dialog">
        <div class="dialog-con comfirmStock">
            <div class="com">入库确认</div>
            <div class="confirmshop">
                <div class="table">
                    <el-table
                    :data="purItemBatchList"
                    border
                    highlight-current-row
                    align="center"
                    style="width: 100%"
                    ref="table">
                    <el-table-column
                    prop="productName"
                    label="商品名称"
                    align="center">
                    </el-table-column>
                    <el-table-column
                    prop="purNumber"
                    label="应入库数"
                    align="center">
                    </el-table-column>
                    <el-table-column
                    prop="totalCount"
                    label="已入库数"
                    align="center" >
                    <template slot-scope="scope">
                        <span>{{scope.row.totalCount == null ? 0 :scope.row.totalCount}}</span>
                    </template>
                    </el-table-column>
                    <el-table-column
                    prop="purNum"
                    label="入库数"
                    align="center">
                    <template slot-scope="scope">
                        <span v-if="scope.row.purNumEdit" class="cell-edit-input">
                            <el-input-number :controls="false" v-model="scope.row.purNum" @blur='handleBlur(scope.row,scope.$index ,"purNumEdit")' placeholder="请输入入库数" style="width: 100%;"  :maxlength="12"></el-input-number>
                        </span>
                        <span v-if="!scope.row.purNumEdit" style="margin-left:10px;" class="cell-icon" @click="handleEdit(scope.row,scope.$index, 'purNumEdit')">{{scope.row.purNum}}</span>
                    </template>
                    </el-table-column>
                    <el-table-column
                    prop="memo"
                    label="备注"
                    align="center">
                    <template slot-scope="scope">
                        <span v-if="scope.row.memoEdit" class="cell-edit-input">
                            <el-input v-model="scope.row.memo" @blur='handleBlur(scope.row,scope.$index ,"memoEdit")' placeholder="请输入备注" ></el-input>
                        </span>
                        <span v-if="!scope.row.memoEdit" style="margin-left:10px;" class="cell-icon" @click="handleEdit(scope.row,scope.$index, 'memoEdit')">{{scope.row.memo}}</span>
                    </template>
                    </el-table-column>
                </el-table>
                </div>
               <div class="stockCon">
                    <el-button @click="conStock(purItemBatchList)" :disabled="disabledConfirm" size="medium" type="primary">确认</el-button>
                    <el-button @click="cancelStock" size="medium">取消</el-button>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import {localStorage} from 'js/localStorage'
import api from 'js/service'
export default {
    inject:['reload'],
    data(){
        return{
            userId:'',
            purData:[],
            dataInfo:[],
            productId:[],
            disabledConfirm:false
        }
    },
    props:{
        purItemBatchList:{
            type:Array,
            default:[]
        },
        shopinfo:{
            type:Object,
            default:{}
        }
    },
    created(){
        this.userId=localStorage.get('userID')
    },
    methods:{
        cancelStock(){
            this.$emit('closeDialog')
        },
        conStock(row){
            this.purData=row
            for(let i in this.purData){
                if(this.purData[i].purNumber < this.purData[i].totalCount){
                    this.dataInfo=[]
                    return this.$message.error('已入库数不能大于应入库数')
                }
                if(this.purData[i].purNumber < Number(this.purData[i].totalCount) + Number(this.purData[i].purNum)){
                    this.dataInfo=[]
                    return this.$message.error('已入库数加入库数不能大于应入库数')
                }
                var obj={
                    purNum:this.purData[i].purNum || 0,
                    memo:this.purData[i].memo,
                    purOrderItemId:this.purData[i].id,
                    productId:this.purData[i].productItemId,
                    purNumber:this.purData[i].purNumber,
                    userId:this.userId
                }
                if(obj.purNum  != 0){
                    this.dataInfo.push(obj)
                }
            }
            let parmas={
                userId:this.userId,
                depotId:this.shopinfo.depotId,
                purItemBatchs:this.dataInfo
            }
            this.$loading.show()
            this.disabledConfirm=true
            let success=data=>{
                if(data.code=="OK"){
                    this.$emit('closeDialog')
                    for(let i in this.purData){
                        this.purData[i].purNum = ''
                    }
                    this.$loading.hide()
                    this.disabledConfirm=false
                    this.reload()
                    return  this.$message.success('确认收货成功')

                }else{
                    for(let i in this.purData){
                        this.purData[i].purNum = ''
                    }
                    this.$loading.hide()
                    this.disabledConfirm=false
                    return this.$message.error(data.msg)
                }
            }
            api.purBatchAdd(parmas,success)

        },
        handleEdit(row,index,editKey){
            let _row = row
            _row[editKey]  = true
            this.$set(this.purItemBatchList, index, _row)
        },
        handleBlur(row,index,editKey) {
            let _row = row
            _row[editKey]  = false
            this.$set(this.purItemBatchList, index, _row)
        },
    }
}
</script>
<style lang="scss" scoped>
.comfirmStock{
    width:600px;
    height:450px;
    .com{
        line-height:40px;
        background: #eee;
        text-align: center;
        margin-bottom:20px;
    }
    .confirmshop{
        padding:0 10px;
    }
    .table{
        height:320px;
        overflow-y: auto;
    }
    .stockCon{
        width:100%;
        text-align:center;
        padding:10px 0;
    }
}
</style>



